<template>
	<view>

		<u-popup :show="show" @close="close"  mode="center" bgColor="transparent">
			<view class="popup-box">
				<view style="width: 600rpx;height: auto;">
					<view class="popup-top">
						<view class="popup-title">确认售出</view>
						<view class="close-btn" @click="close()">
							<image src="/static/icon-images/close.png" mode="widthFix"></image>
						</view>
					</view>


					<!-- 内容区 -->
					<view class="input-field pic-box">
						<image src="/static/public-images/user-avar.png" class="product-pic"></image>
						<view class="right">
							<view class="name">{{setType(type)}}</view>
							<view style="display: flex;align-items: center;gap: 20rpx;">
								<view class="text">单价：{{price || 0}}</view>
							</view>
							<view style="display: flex;align-items: center;">
								<view class="text">数量：</view>
								<u-number-box :max="sum" v-model="value" ></u-number-box>
							</view>
						</view>
					</view>

					<!-- <view class="warn-box">
					
						<u-icon name="warning-fill" color="#f87171" size="20"></u-icon>
						<view class="warn-text">售出后无法退款，请确认交易信息</view>
					</view> -->
					
					<view class="warn-box">
						<u-icon name="info-circle-fill" color="#f1b722" size="15"></u-icon>
						<view class="warn-text">预计收益：{{(price*value*(1-issionRate)).toFixed(4)}}</view>
					</view>

					<view class="btn-group">
						<view class="false-btn" @click="close()">取消</view>
						<view class="true-btn" @click="submit">确认售出</view>
					</view>

				</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	import {productSold,commissionRate} from '@/api/market.js'
	export default {
		data() {
			return {
				sum:1,
				price:null,
				type:null,
				value: 1,
				show: false,
				id: null,
				issionRate:null
			};
		},
		methods: {
			async getCommissionRate(){
				let res = await commissionRate()
				this.issionRate = res.data
			},
			open(item) {
				this.getCommissionRate()
				console.log('buy-item:', item);
				this.value = 1
				this.type = item.type
				this.id = item.productId
				this.price = item.price
				this.sum = item.skus
				this.show = true
			},
			close() {
				this.show = false
			},
			
			setType(type) {
				switch (type) {
					case 1:
						return '1%合成石'
						break;
					case 2:
						return '2%合成石'
						break;
					case 3:
						return '5%合成石'
						break;
					case 4:
						return '10合成石%'
						break;
					case 5:
						return '20%合成石'
						break;
					case 6:
						return '50%合成石'
						break;
					case 7:
						return '5%合成石'
						break;
					case 8:
						return '积分'
						break;
					case 9:
						return '宝箱'
						break;
					default:
						return '神秘商品'
				}
			},
			async submit() {
				try {
					let data={
						number:this.value,
						productId:this.id
					}
					let res = await productSold(data)
					this.show = false
					this.$emit('success')
					uni.showToast({
						title:'售出成功',
						icon:'none'
					})
				} catch (error) {
					//TODO handle the exception
				}

			}
		}
	}
</script>

<style lang="scss">
	.popup-box {
		background: linear-gradient(135deg, #0a1a3a 0%, #050a1a 100%);
		border: 1rpx solid rgba(255, 255, 255, 0.15);
		border-radius: 20rpx;
		overflow-y: auto;
		padding: 30rpx;
	}

	.popup-top {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.popup-title {
		font-weight: 700;
		font-size: 38rpx;
		color: #e2e8f0;
	}

	.close-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;
		background: rgba(255, 255, 255, 0.15);

		image {
			display: block;
			width: 28rpx;
			height: 28rpx;
		}
	}

	.product-pic {
		display: block;
		width: 150rpx;
		height: 150rpx;
		border-radius: 10rpx;
	}

	.pic-box {
		margin-top: 30rpx;
		padding: 30rpx;
		display: flex;
		align-items: center;
	}

	.name {
		line-height: 1;
		font-size: 36rpx;
		color: #fff;
		font-weight: 700;
	}

	.text {
		line-height: 1;
		font-size: 28rpx;
		font-weight: 400;
		color: #9ca3af;
	}

	.sum {
		line-height: 1;
		font-size: 30rpx;
		font-weight: 500;
		color: #ff9800;
		// text-shadow: 0 0 5rpx rgba(255, 152, 0, 0.8), 0 0 10rpx rgba(255, 152, 0, 0.6);
	}

	.right {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
		margin-left: 30rpx;
	}

	.warn-box {
		margin-top: 20rpx;
		display: flex;
		align-items: center;
	}

	.warn-text {
		line-height: 1;
		font-size: 28rpx;
		font-weight: 500;
		// color: #f87171;
		color: #f1b722;
		margin-left: 10rpx;
	}

	.btn-group {
		margin-bottom: 20rpx;
		margin-top: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.false-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 36rpx;
		color: #e2e8f0;
		background-color: #374151;
		border-radius: 10rpx;
		padding: 20rpx 94rpx;
	}

	.true-btn {
		padding: 20rpx 94rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 36rpx;
		color: #fff;
		background-color: #4caf50;
		border-radius: 10rpx;
	}
</style>